﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BottomViewCtrl : MonoBehaviour
{

    #region 数据定义
    public static BottomViewCtrl mInstance;

    public event OnChooseOneItem OnChooseOneItemEvent;
    public delegate void OnChooseOneItem(Transform oItem);

    public event OnUnchooseOneItem OnUnchooseOneItemEvent;
    public delegate void OnUnchooseOneItem(Transform oItem);

    private GameObject mOpenViewButton, mBottomView, mLeftTitleImage, mRightCornerImage, mViewContent;

    private RectTransform mViewRectTransform, mContentTransform;
    private GridLayoutGroup mGridLayout;

    private Vector2 mExpandTargetPos, mHideTargetPos, mCurTargetPos;
    private bool mMoving, mShowRightImage, mMoveByOnce;

    private Vector2 mContentSize;

    #endregion

    #region Unity自带方法
    void Awake()
    {
        mInstance = this;
        FindGo();
        BindEvent();
        InitlData();
    }

	// Use this for initialization
	void Start () {
        
        
	}
	
	// Update is called once per frame
	void Update ()
    {
        //处理展开/隐藏的过程
        if (mMoving)
        {
            if (!mMoveByOnce && Vector2.Distance(mViewRectTransform.anchoredPosition, mCurTargetPos) > 0.1f)
            {
                mViewRectTransform.anchoredPosition = Vector2.Lerp(mViewRectTransform.anchoredPosition,
                    mCurTargetPos, Time.deltaTime);
                print("moving");
            }
            else
            {
                mViewRectTransform.anchoredPosition = mCurTargetPos;
                mMoving = false;
                if (mCurTargetPos.Equals(mHideTargetPos))
                {
                    mOpenViewButton.SetActive(true);
                    mRightCornerImage.SetActive(false);
                }
            }
        }
	}

    #endregion

    #region 私有方法
    private void FindGo()
    {
        if (mOpenViewButton == null)
        {
            mOpenViewButton = TransformHelper.FindChild(transform, "OpenViewButton");
        }
        if (mBottomView == null)
        {
            mBottomView = TransformHelper.FindChild(transform, "BottomView");
        }
        if (mViewRectTransform == null)
        {
            mViewRectTransform = mBottomView.transform as RectTransform;
        }
        if (mViewContent == null)
        {
            mViewContent = TransformHelper.FindChild(transform, "Content");
        }
        if (mContentTransform == null)
        {
            mContentTransform = mViewContent.transform as RectTransform;
        }
        if (mGridLayout == null)
        {
            mGridLayout = mViewContent.GetComponent<GridLayoutGroup>();
        }
        if (mLeftTitleImage == null)
        {
            mLeftTitleImage = TransformHelper.FindChild(transform, "LeftTitleImage");
        }
        if (mRightCornerImage == null)
        {
            mRightCornerImage = TransformHelper.FindChild(transform, "RightCornerImage");
        }
    }

    private void BindEvent()
    {
        mOpenViewButton.GetComponent<Button>().onClick.AddListener(delegate() { OnClickOpenViewEvent(); });
        mRightCornerImage.GetComponent<Button>().onClick.AddListener(delegate() { OnClickHideViewEvent(); });
    }

    private void InitlData()
    {
        Vector2 oSize = mViewRectTransform.sizeDelta;
        mHideTargetPos = new Vector2(-0.5f * oSize.x, 0.5f * oSize.y);
        mExpandTargetPos = new Vector2(0.5f * oSize.x, 0.5f * oSize.y);
        mMoving = false;
        mMoveByOnce = false;
        mContentSize = mContentTransform.sizeDelta;
    }

    private void OnClickOpenViewEvent()
    {
        mOpenViewButton.gameObject.SetActive(false);
        mRightCornerImage.SetActive(mShowRightImage);
        mCurTargetPos = mExpandTargetPos;
        mMoving = true;
    }

    private void OnClickHideViewEvent()
    {
        mCurTargetPos = mHideTargetPos;
        mMoving = true;
    }

    #endregion


    #region Public接口方法

    /// <summary>
    /// 设置抽屉的状态
    /// </summary>
    /// <param name="bInitlHide">初始状态是否为隐藏状态</param>
    /// <param name="bCanHide">是否允许展开/隐藏的状态切换</param>
    /// <param name="bShowHideImage">是否显示隐藏按钮</param>
    public void SetScrollStatus(bool bInitlHide, bool bCanHide, bool bShowHideImage, bool bMoveByOnce)
    {
        if (bInitlHide)
        {
            mOpenViewButton.SetActive(true);
            mViewRectTransform.anchoredPosition = mHideTargetPos;
            mRightCornerImage.SetActive(false);
        }
        else
        {
            mOpenViewButton.SetActive(false);
            mViewRectTransform.anchoredPosition = mExpandTargetPos;
            mRightCornerImage.SetActive(bShowHideImage);
        }

        mMoveByOnce = bMoveByOnce;
        mShowRightImage = bShowHideImage;
        mRightCornerImage.GetComponent<Button>().enabled = bCanHide;
    }

    /// <summary>
    /// 设置左侧Title（自定义）
    /// </summary>
    /// <param name="oTexture">图片</param>
    /// <param name="dLeftOffset">左边距</param>
    /// <param name="dRightOffset">右边距</param>
    public void SetLeftTitle(Sprite oTexture, float dLeftOffset, float dRightOffset)
    {
        mLeftTitleImage.SetActive(true);
        //设置图片
        Image oImage = mLeftTitleImage.GetComponent<Image>();
        oImage.sprite = oTexture;
        oImage.SetNativeSize();
        RectTransform oTransform = mLeftTitleImage.transform as RectTransform;
        oTransform.anchoredPosition = oTransform.anchoredPosition + new Vector2(dLeftOffset, 0);

        //根据图片大小及左右边距，调整ScrollView的左边距
        RectOffset oPadding = mViewContent.GetComponent<GridLayoutGroup>().padding;
        float dOffset = oTransform.sizeDelta.x + dLeftOffset + dRightOffset;
        if (oPadding.left < dOffset)
        {
            oPadding.left = (int)dOffset;
            mGridLayout.padding = oPadding;
        }       
    }

    /// <summary>
    /// 设置左侧Title（默认图片）
    /// </summary>
    /// <param name="oTexture">图片</param>
    /// <param name="dLeftOffset">左边距</param>
    /// <param name="dRightOffset">右边距</param>
    public void SetLeftTitleByDefault(bool bShow)
    {
        mLeftTitleImage.SetActive(bShow);
        if (bShow)
        {
            RectTransform oTransform = mLeftTitleImage.transform as RectTransform;
            RectOffset oPadding = mViewContent.GetComponent<GridLayoutGroup>().padding;
            oPadding.left = 80;
            mViewContent.GetComponent<GridLayoutGroup>().padding = oPadding;
        }
    }

    /// <summary>
    /// 设置ScrollView的布局
    /// </summary>
    /// <param name="bCanMove">是否允许左右滑动</param>
    /// <param name="oCellSize">单个Item的大小</param>
    /// <param name="oSpacing">Item之间的间隔</param>
    /// <param name="oPadding">距离边缘的位置</param>
    public void SetScrollViewLayout(bool bCanMove, Vector2 oCellSize, Vector2 oSpacing, RectOffset oPadding)
    {
        mBottomView.GetComponent<ScrollRect>().enabled = bCanMove;
        GridLayoutGroup oLayout = mViewContent.GetComponent<GridLayoutGroup>();
        if (oCellSize != null)
        {
            oLayout.cellSize = oCellSize;
        }
        if (oSpacing != null)
        {
            oLayout.spacing = oSpacing;
        }
        if (oPadding != null)
        {
            oLayout.padding = oPadding;
        }        
    }    

    /// <summary>
    /// 添加一个Item
    /// </summary>
    /// <param name="oItem"></param>
    public void AddViewItem(GameObject oItem)
    {        
        oItem.transform.parent = mViewContent.transform;
        RectTransform oTransform = oItem.transform as RectTransform;
        oTransform.localPosition = new Vector3(oTransform.localPosition.x, oTransform.localPosition.y, 0);
        oTransform.localScale = Vector3.one;

        float dWidth = mViewContent.transform.childCount * mGridLayout.cellSize.x + 
            (mViewContent.transform.childCount - 1)  * mGridLayout.spacing.x
            + mGridLayout.padding.left + mGridLayout.padding.right;

        mContentTransform.sizeDelta = new Vector2(dWidth, mContentTransform.sizeDelta.y); 
    }

    public void SetChoosenItem(GameObject oItem)
    {
        for (int i = 0; i < mContentTransform.childCount; i++)
        {
            if (oItem == mContentTransform.GetChild(i).gameObject)
            {
                OnChooseOneItemEvent(oItem.transform);
            }
            else
            {
                OnUnchooseOneItemEvent(mContentTransform.GetChild(i));
            }
        }
    }

    #endregion

}